package com.jwaoo.chat.session.avchat;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;

import com.dx.as.common.CommonUtils;
import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.chat.R;
import com.jwaoo.chat.activity.module.fromapp.AppInstance;

/**
 * Created by jwaoo on 2017/11/9.
 */

public class AVChatDialog {

    private static AVChatDialog dialog;
    public static AVChatDialog getInstance(){
        if (dialog == null){
            synchronized (AVChatDialog.class){
                if (dialog == null){
                    dialog = new AVChatDialog();
                }
            }
        }
        return dialog;
    }

    private View baseView;
    public View getBaseView(){
        return baseView;
    }

    public void showDialog(final WindowManager windowManager, int type, @Nullable final View.OnClickListener onClickListener1,@Nullable final View.OnClickListener onClickListener2){

        baseView = View.inflate(ChatHelpter.getContext(), R.layout.avchat_dialog,null);

        if (type == 0)
            baseView.findViewById(R.id.view_error).setVisibility(View.VISIBLE);
        else if(type == 1)
            baseView.findViewById(R.id.view_request).setVisibility(View.VISIBLE);
        else if (type == 2){
            View v = baseView.findViewById(R.id.view_reject);
            v.setVisibility(View.VISIBLE);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    closeMyDialog(windowManager,baseView);
                }
            });
        }

        baseView.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeMyDialog(windowManager,baseView);
            }
        });
        baseView.findViewById(R.id.btn_refuse).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeMyDialog(windowManager,baseView);
                if (onClickListener1 != null)
                    onClickListener1.onClick(v);
            }
        });
        baseView.findViewById(R.id.btn_receive).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeMyDialog(windowManager,baseView);
                if (onClickListener2 != null)
                    onClickListener2.onClick(v);
            }
        });
        if (CommonUtils.isEnFloat()){
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            layoutParams.format = PixelFormat.TRANSLUCENT;
            layoutParams.type= WindowManager.LayoutParams.TYPE_TOAST;
            windowManager.addView(baseView,layoutParams);
        }else {
            MyDialogActivity.showMyDialog();
        }

    }

    private static void closeMyDialog(WindowManager windowManager,View baseView){
        if (CommonUtils.isEnFloat()){
            windowManager.removeView(baseView);
        }else {
            MyDialogActivity.closeMyDialog();
        }
        baseView = null;
    }

}
